import { message } from 'antd';
// import { useModel } from 'umi';
import type { CreateLogisticsType } from '../data';
import { ModalForm, ProFormDigit, ProFormRadio, ProFormText } from '@ant-design/pro-form';
import { addLogisticsConfig } from '../service';

interface undateParkList {
  visible: boolean;
  unVisible: () => void;
  reloadFunc: () => void;
}
export default (props: undateParkList) => {
  const { visible, unVisible, reloadFunc } = props;
  const compareArray = (fW: number, wN: number[]) => {
    if (wN[0] > fW) {
      for (let i = 1; i < wN.length; i++) {
        if (wN[i - 1] > wN[i]) {
          return false;
        }
      }
      return true;
    } else {
      return false;
    }
  };
  return (
    <div>
      <ModalForm
        style={{ marginLeft: 65 }}
        modalProps={{
          destroyOnClose: true,
          onCancel: () => {
            unVisible();
          },
          centered: true,
          footer: null,
        }}
        layout="horizontal"
        onFinish={async (value: CreateLogisticsType) => {
          const { weight } = value;
          const weightString = weight.split('/');
          const weightNumber = [];
          for (let i = 0; i < weightString.length; i++) {
            for (let j = 0; j < weightString[i].length; j++) {
              if (!/^[0-9]$/.test(weightString[i][j])) {
                message.error(`重量区间格式不正确，请以'数字/数字/数字'的形式输入`);
                return false;
              }
            }
            weightNumber[i] = Number(weightString[i]);
          }
          const values = {
            ...value,
            weightInterval: weightNumber,
          };
          if (compareArray(value.startingWeight, values.weightInterval)) {
            const hide = message.loading('正在添加');
            const res = await addLogisticsConfig(values);
            hide();
            if (res.code === 0) {
              message.success(`添加成功`);
              reloadFunc();
              unVisible();
              return true;
            } else {
              message.error(`添加失败,${res.message}`);
              return false;
            }
          } else {
            message.error('起重重量大于区间重量或区间重量输入错误，请重新输入');
            return false;
          }
        }}
        visible={visible}
        title="添加物流配置"
        width={650}
      >
        <ProFormText
          required
          width={377}
          label="物流名称"
          placeholder="请输入物流名称"
          name="logisticsName"
          rules={[{ required: true, message: '请输入' }]}
        />
        <ProFormDigit
          fieldProps={{
            addonAfter: 'kg',
            precision: 2,
          }}
          width={377}
          required
          label="起步重量"
          placeholder="请输入起步重量"
          name="startingWeight"
          rules={[{ required: true, message: '请输入' }]}
        />
        <ProFormText
          required
          width={377}
          label="重量区间"
          placeholder="除首重外每个区间的末尾重量   以/间隔输入"
          name="weight"
          rules={[{ required: true, message: '请输入' }]}
        />
        <ProFormDigit
          fieldProps={{
            addonAfter: '折',
            precision: 1,
          }}
          width={377}
          max={10}
          min={0.1}
          initialValue={10}
          required
          label="省内打折"
          name="provinceDiscount"
          rules={[{ required: true, message: '请输入' }]}
        />
        <ProFormDigit
          min={0.1}
          max={10}
          fieldProps={{
            addonAfter: '折',
            precision: 1,
          }}
          width={377}
          initialValue={10}
          required
          label="省外打折"
          name="outsideDiscount"
          rules={[{ required: true, message: '请输入' }]}
        />
        <ProFormRadio.Group
          radioType="radio"
          width={377}
          name="chargeType"
          label="是否有最低收费"
          // fieldRef={Parktype}
          options={[
            {
              label: '没有最低收费',
              value: 1,
            },
            {
              label: '有最低收费',
              value: 2,
            },
          ]}
          rules={[{ required: true, message: '请选择' }]}
        />
        <ProFormRadio.Group
          radioType="radio"
          width={377}
          name="chargeWay"
          label="收费方式"
          // fieldRef={Parktype}
          options={[
            {
              label: '分段收费',
              value: 1,
            },
            {
              label: '对段加首重',
              value: 2,
            },
          ]}
          rules={[{ required: true, message: '请选择' }]}
        />
        <ProFormRadio.Group
          radioType="radio"
          name="weightRules"
          width={377}
          label="重量进位规则"
          // fieldRef={Parktype}
          options={[
            {
              label: '无规则',
              value: 1,
            },
            {
              label: '四舍五入取整数',
              value: 2,
            },
            {
              label: '进位至下一个0.5kg',
              value: 3,
            },
          ]}
          rules={[{ required: true, message: '请选择' }]}
        />
        <ProFormRadio.Group
          radioType="radio"
          width={377}
          name="shippingRules"
          label="运费进位规则"
          // fieldRef={Parktype}
          options={[
            {
              label: '无规则',
              value: 1,
            },
            {
              label: '四舍五入取整数',
              value: 2,
            },
          ]}
          rules={[{ required: true, message: '请选择' }]}
        />
      </ModalForm>
    </div>
  );
};
